package xdpm.nhom16.DAO;

import java.util.ArrayList;

import xdpm.nhom16.DTO.DuongDTO;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DuongDAO extends AbstractDAO<DuongDTO> {

	public static final String COLUMN_ID = "MaDuong";
	public static final String[] NAME_COLUMNS = new String[] { COLUMN_ID, "TenDuong" };
	public static final String TABLE_NAME = "DUONG";

	public DuongDAO(SQLiteDatabase db) {
		super(db);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected String getTableName() {
		// TODO Auto-generated method stub
		return TABLE_NAME;
	}

	@Override
	protected String getNameColumnId() {
		// TODO Auto-generated method stub
		return COLUMN_ID;
	}

	@Override
	protected String[] getNameColumns() {
		// TODO Auto-generated method stub
		return NAME_COLUMNS;
	}

	@Override
	protected DuongDTO cusorToDTO(Cursor cursor) {
		// TODO Auto-generated method stub
		DuongDTO result = new DuongDTO();
		result.setMaDuong(cursor.getInt(0));
		if (!cursor.isNull(1))
			result.setTenDuong(cursor.getString(1));
		return result;
	}

	public int GetIdByName(String name) {
		int result = -1;
		try {
			Cursor cursor = database.query(getTableName(), getNameColumns(),
					"TenDuong='" + name + "'", null, null, null, null);
			if (cursor != null) {
				cursor.moveToFirst();
				if (!cursor.isAfterLast())
					result = cursor.getInt(0);
			}
		} catch (Exception e) {
			result = -1;
		}
		return result;
	}
	public ArrayList<String> getTenDuong (){
		ArrayList<String> listDuong = new ArrayList<String>();
		String query = String.format("SELECT TenDuong FROM DUONG");
		Cursor cursor = database.rawQuery(query, null);
		if (null == cursor || !cursor.moveToFirst())
			return null;
		while (!cursor.isAfterLast()) {
			if (!cursor.isNull(0)) {
				String duong = cursor.getString(0);
				listDuong.add(duong);
			}
			cursor.moveToNext();
		}
	
		return listDuong;	
	}
}
